package com.yzdbx.task;

import cn.hutool.core.util.RandomUtil;
import com.yzdbx.utils.DbUtil;
import com.yzdbx.utils.GenUtil;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: SERVEVICE-DATA-GEN
 * @BelongsPackage: com.yzdbx.task
 * @Author: lk
 * @CreateTime: 2025-05-14
 * @Description: TODO
 * @Version: 2.0
 * 中兴木叶
 */
@Slf4j
public class MainTask implements Runnable {

    public static LocalDate start = LocalDate.of(2025,4,13);
    public static LocalDate end = LocalDate.of(2025,7,28);

    @Override
    public void run() {


//        LocalDate start = LocalDate.now();
//        LocalDate end = LocalDate.now();


        if (GenUtil.IS_INIT.equals("N")){
            start = LocalDate.now();
            end = LocalDate.now();
        }
        long startTimeStamp = System.currentTimeMillis();

        Connection connection = DbUtil.getConnection();
        try {
            while (!start.isAfter(end)) {
                // 跳过2025年1月28号到2025年2月4号的数据生成
                LocalDate skipStart = LocalDate.of(2025, 1, 28);
                LocalDate skipEnd = LocalDate.of(2025, 2, 4);
                if (!start.isBefore(skipStart) && !start.isAfter(skipEnd)) {
                    log.info("跳过日期: {}", start);
                    start = start.plusDays(1);
                    continue;
                }
                
                GenUtil.setCurDate(start);
                GenUtil.setCurTimeStamp(new Timestamp(start.atStartOfDay(ZoneId.systemDefault())
                        .toInstant()
                        .toEpochMilli() + RandomUtil.randomLong(21600000, 72000000)));
                executorTask(connection);
                start = start.plusDays(1);
            }
            connection.commit();
            log.info("========================= 数据生成成功====================耗时:{}毫秒",  (System.currentTimeMillis() - startTimeStamp));
        } catch (Exception ex) {
            log.error("======================= 数据生成失败 ======================", ex);
            try {
                connection.rollback();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        } finally {

            DbUtil.closeConnection(connection);
        }
    }

    private static void executorTask(Connection connection) {
        try {
            List<Boolean> results = new ArrayList<>();

            //随机的生成条数
            int count = RandomUtil.randomInt(1, 4);
//            int count = RandomUtil.randomInt(2, 9);

            results.add(new SalefOrderRunnable(connection).execute(count));
            results.add(new SaleOrderFaHuoRunnable(connection).execute(count));
            results.add(new PurchaseRunnable(connection).execute(count));
            results.add(new PurchaseShouHuoRunnable(connection).execute(count));
            results.add(new ProductPlanRunnable(connection).execute(count));
            results.add(new ProductOrderRunnable(connection).execute(count));
            results.add(new MaintenanceTaskRunnable(connection).execute(count));
            results.add(new PointInspectionTaskRunnable(connection).execute(count));
            results.add(new TroubleShootingTaskRunnable(connection).execute(count));
            results.add(new AfterSaleRunnable(connection).execute(count));
            results.add(new FinishedProductInspectionRunnable(connection).execute(count));
            results.add(new LLOutRunnable(connection).execute(count));
            results.add(new FinishProductIntoRunnable(connection).execute(count));

            if (results.stream().anyMatch(result
                    -> result.equals(Boolean.FALSE))) {
                throw new RuntimeException("数据生成失败");
            }

        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


}
